#include <string.h>
#include <errno.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/logging/log.h>

#include "serial.h"
#include "hwap_protocol.h"

#define LOG_MODULE_NAME main
LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG);

struct serial_inst *serial_inst = NULL;

static void serial_cb(const uint8_t *const data, uint16_t len)
{
	// LOG_HEXDUMP_DBG(data, len, "RX");
	hwap_decode_byte(data, len);
}

int main(void)
{
	serial_inst = serial_init((rx_proccess_t)serial_cb);
	if (!serial_inst)
	{
		LOG_ERR("Failed to init serial lib.");
		return -1;
	}

	LOG_INF("Serial lib example start.");

	// uint8_t tx_data[] = {0x48, 0x57, 0x65, 0x72, 0x00, 0x02, 0x00, 0x01, 0x10, 0x01, 0x00, 0x00, 0x32, 0x42};
	uint8_t tx_data[75] = {0};
	uint8_t version_data[] = {0x48, 0x57, 0x65, 0x72, 0x20, 0x00, 0x00, 0x01, 0x10, 0x02, 0x00, 0x3d, 0x02, 0x66, 0x84, 0x53, 0x46, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x2e, 0x34, 0x47};
	memcpy(tx_data, version_data, sizeof(version_data));
	tx_data[73] = 0x8d;
	tx_data[74] = 0xdf;
	for (;;)
	{
		// serial_inst->tx(tx_data, sizeof(tx_data));
		k_msleep(5000);
	}

	return 0;
}
